import SwiftUI

struct PrimaryButton: View {
    var title: String
    var action: () -> Void
    var isEnabled: Bool = true
    var isLoading: Bool = false
    var size: ButtonSize = .medium
    
    enum ButtonSize { case small, medium, large }
    
    var body: some View {
        Button(action: action) {
            HStack {
                if isLoading {
                    ProgressView()
                        .progressViewStyle(CircularProgressViewStyle())
                }
                Text(title)
                    .font(font)
                    .padding(.horizontal, paddingH)
                    .padding(.vertical, paddingV)
            }
            .frame(maxWidth: .infinity)
            .background(isEnabled ? Color.accentColor : Color.gray.opacity(0.3))
            .foregroundColor(.white)
            .cornerRadius(12)
            .opacity(isEnabled ? 1 : 0.6)
            .scaleEffect(isEnabled ? 1 : 0.98)
            .animation(.easeInOut(duration: 0.15), value: isEnabled)
        }
        .disabled(!isEnabled || isLoading)
    }
    
    private var font: Font {
        switch size {
        case .small: return .system(size: 14, weight: .semibold)
        case .medium: return .system(size: 16, weight: .semibold)
        case .large: return .system(size: 18, weight: .bold)
        }
    }
    private var paddingH: CGFloat {
        switch size {
        case .small: return 16
        case .medium: return 20
        case .large: return 24
        }
    }
    private var paddingV: CGFloat {
        switch size {
        case .small: return 8
        case .medium: return 12
        case .large: return 16
        }
    }
}

#Preview {
    VStack(spacing: 16) {
        PrimaryButton(title: "主按钮", action: {})
        PrimaryButton(title: "加载中", action: {}, isLoading: true)
        PrimaryButton(title: "禁用", action: {}, isEnabled: false)
    }.padding()
} 